cmake_minimum_required(VERSION 3.17)

project(slidewincropimagebackend LANGUAGES C CXX)

option(TRITON_ENABLE_GPU "Enable GPU support in backend" ON)
option(TRITON_ENABLE_STATS "Include statistics collections in backend" ON)

set(CMAKE_BUILD_TYPE Release)

include(FetchContent)

FetchContent_Declare(
  repo-common
  GIT_REPOSITORY https://gitee.com/luo_zhi_cheng/triton-common
  GIT_TAG "r23.12"
  GIT_SHALLOW ON
)
FetchContent_Declare(
  repo-core
  GIT_REPOSITORY https://gitee.com/luo_zhi_cheng/triton-core
  GIT_TAG "r23.12"
  GIT_SHALLOW ON
)
FetchContent_Declare(
  repo-backend
  GIT_REPOSITORY https://gitee.com/luo_zhi_cheng/triton-backend
  GIT_TAG "r23.12"
  GIT_SHALLOW ON
)
FetchContent_MakeAvailable(repo-common repo-core repo-backend)

####################################################################################################
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS}  -fpic")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}  -fpic")
option(CUDA_USE_STATIC_CUDA_RUNTIME OFF)
if ("${GPU_CC}" STREQUAL "")
set(GPU_CC 89)
endif()
set(CUDA_GEN_CODE "-gencode=arch=compute_${GPU_CC},code=sm_${GPU_CC}")
message(${CUDA_GEN_CODE})
# set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -Wall -O0 -Wfatal-errors -pthread -w -g")
# set(CUDA_NVCC_FLAGS "${CUDA_NVCC_FLAGS} -std=c++11 -O0 -Xcompiler -fPIC -g -w ${CUDA_GEN_CODE}")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++17 -Wall -O3 -Wfatal-errors -pthread -w")
set(CUDA_NVCC_FLAGS "${CUDA_NVCC_FLAGS} -std=c++17 -O3 -Xcompiler -fPIC -w ${CUDA_GEN_CODE}")
find_package(CUDA REQUIRED)
include_directories(${CUDA_INCLUDE_DIRS})
####################################################################################################

####################################################################################################
#common_func
set(CUSTOM_TENSOR_DIR   "${PROJECT_SOURCE_DIR}/../cus_tensor/core")
message("${CUSTOM_TENSOR_DIR}")
include_directories(${CUSTOM_TENSOR_DIR})
file(GLOB_RECURSE cpp_srcs ${CUSTOM_TENSOR_DIR}/*.cpp)
set(ITensorCppSo "ITensorCpp")
#add_library(${ITensorCppSo} SHARED ${cpp_srcs})
add_library(${ITensorCppSo} ${cpp_srcs})
target_link_libraries(${ITensorCppSo} ${CUDA_LIBRARIES} cuda)
target_link_libraries(${ITensorCppSo} pthread)
####################################################################################################

####################################################################################################
#cuda_kernel
set(CUSTOM_KERNEL_DIR   "${PROJECT_SOURCE_DIR}/../../../kernel")
include_directories(${CUSTOM_KERNEL_DIR})
file(GLOB_RECURSE cu_files1 ${CUSTOM_KERNEL_DIR}/*.cu)
set(kernelCuSo "kernelCu")
#cuda_add_library(${kernelCuSo} SHARED ${cu_files1})
cuda_add_library(${kernelCuSo} ${cu_files1})
target_link_libraries(${kernelCuSo} ${CUDA_LIBRARIES} cuda ${CUDA_cublas_LIBRARIES})
####################################################################################################

####################################################################################################
#defintion
if ("${USE_TO_CPU}" STREQUAL "")
set(USE_TO_CPU 0)
endif()
if(${USE_TO_CPU} EQUAL 0)
    message("NOT BACKEND_TO_CPU")
else()
    message("BACKEND_TO_CPU")
    add_definitions(-DBACKEND_TO_CPU)
endif()
####################################################################################################

configure_file(src/libtriton_slidewincropimage.ldscript libtriton_slidewincropimage.ldscript COPYONLY)

add_library(
  triton-slidewincropimage-backend SHARED
  src/slidewincropimage.cc
)

add_library(
  SlideWinCropImageBackend::triton-slidewincropimage-backend ALIAS triton-slidewincropimage-backend
)

target_include_directories(
  triton-slidewincropimage-backend
  PRIVATE
    ${CMAKE_CURRENT_SOURCE_DIR}/src
)

target_compile_features(triton-slidewincropimage-backend PRIVATE cxx_std_11)
target_compile_options(
  triton-slidewincropimage-backend PRIVATE
  $<$<OR:$<CXX_COMPILER_ID:Clang>,$<CXX_COMPILER_ID:AppleClang>,$<CXX_COMPILER_ID:GNU>>:
    -Wall -Wextra -Wno-unused-parameter -Wno-type-limits -Werror>
  $<$<CXX_COMPILER_ID:MSVC>:/Wall /D_WIN32_WINNT=0x0A00 /EHsc /Zc:preprocessor>
)

target_link_libraries(
  triton-slidewincropimage-backend
  PRIVATE
    triton-core-serverapi   # from repo-core
    triton-core-backendapi  # from repo-core
    triton-core-serverstub  # from repo-core
    triton-backend-utils    # from repo-backend
    ${ITensorCppSo}
    ${kernelCuSo}
)

if(WIN32)
  set_target_properties(
    triton-slidewincropimage-backend PROPERTIES
    POSITION_INDEPENDENT_CODE ON
    OUTPUT_NAME triton_slidewincropimage
  )
else()
  set_target_properties(
    triton-slidewincropimage-backend PROPERTIES
    POSITION_INDEPENDENT_CODE ON
    OUTPUT_NAME triton_slidewincropimage
    LINK_DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/libtriton_slidewincropimage.ldscript
    LINK_FLAGS "-Wl,--version-script libtriton_slidewincropimage.ldscript"
  )
endif()

#
# Install
#
include(GNUInstallDirs)
set(INSTALL_CONFIGDIR ${CMAKE_INSTALL_LIBDIR}/cmake/SlideWinCropImageBackend)

install(
  TARGETS
    triton-slidewincropimage-backend
  EXPORT
    triton-slidewincropimage-backend-targets
  LIBRARY DESTINATION ${CMAKE_INSTALL_PREFIX}/backends/slidewincropimage
  RUNTIME DESTINATION ${CMAKE_INSTALL_PREFIX}/backends/slidewincropimage
)

install(
  EXPORT
    triton-slidewincropimage-backend-targets
  FILE
    SlideWinCropImageBackendTargets.cmake
  NAMESPACE
    SlideWinCropImageBackend::
  DESTINATION
    ${INSTALL_CONFIGDIR}
)

include(CMakePackageConfigHelpers)
configure_package_config_file(
  ${CMAKE_CURRENT_LIST_DIR}/cmake/SlideWinCropImageBackendConfig.cmake.in
  ${CMAKE_CURRENT_BINARY_DIR}/SlideWinCropImageBackendConfig.cmake
  INSTALL_DESTINATION ${INSTALL_CONFIGDIR}
)

install(
  FILES
  ${CMAKE_CURRENT_BINARY_DIR}/SlideWinCropImageBackendConfig.cmake
  DESTINATION ${INSTALL_CONFIGDIR}
)

#
# Export from build tree
#
export(
  EXPORT triton-slidewincropimage-backend-targets
  FILE ${CMAKE_CURRENT_BINARY_DIR}/SlideWinCropImageBackendTargets.cmake
  NAMESPACE SlideWinCropImageBackend::
)

export(PACKAGE SlideWinCropImageBackend)
